﻿$(function () {

    var url = '/EasyUI/GetTreeGrid'
    $('#test').treegrid({
        nowrap: false,
        animate: true,
        collapsible: true,
        url: url,
        idField: 'id',
        treeField: 'text',
        columns: [[
            { field: 'text', title: '作业', width: 120 },
            { field: 'value', title: '值', width: 120 },
            { field: 'hasLogic', title: '是否有逻辑', width: 150,
                formatter: function (value) {
                    if (value)
                        return '<span style="color:red">' + value + '</span>';
                    else
                        return '';
                }
            }
        ]],
        onSelect: function (row) {
            if (row) {
                alert(row.id);
            }
        }
    });
});

function reload() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('reload', node.code);
    } else {
        $('#test').treegrid('reload');
    }
}
function getChildren() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        var nodes = $('#test').treegrid('getChildren', node.code);
    } else {
        var nodes = $('#test').treegrid('getChildren');
    }
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        s += nodes[i].code + ',';
    }
    alert(s);
}
function getSelected() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        alert(node.code + ":" + node.name);
    }
}
function collapse() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('collapse', node.code);
    }
}
function expand() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('expand', node.code);
    }
}
function collapseAll() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('collapseAll', node.code);
    } else {
        $('#test').treegrid('collapseAll');
    }
}
function expandAll() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('expandAll', node.code);
    } else {
        $('#test').treegrid('expandAll');
    }
}
function expandTo() {
    $('#test').treegrid('expandTo', '02013');
    $('#test').treegrid('select', '02013');
}
var codeIndex = 1000;
function append() {
    codeIndex++;
    var data = [{
        code: 'code' + codeIndex,
        name: 'name' + codeIndex,
        addr: 'address' + codeIndex,
        col4: 'col4 data'
    }];
    var node = $('#test').treegrid('getSelected');
    $('#test').treegrid('append', {
        parent: (node ? node.code : null),
        data: data
    });
}
function remove() {
    var node = $('#test').treegrid('getSelected');
    if (node) {
        $('#test').treegrid('remove', node.code);
    }
}